/**
 * edurender
 * er.h
 * (c) 2009 J. Brad Byrd
 */

#ifndef EDURENDER_ER_H
#define EDURENDER_ER_H

#ifdef __cplusplus
extern "C"
{
#endif

typedef signed __int8 ERbyte;
typedef unsigned __int8 ERubyte;
typedef signed __int16 ERshort;
typedef unsigned __int16 ERushort;
typedef signed __int32 ERint;
typedef unsigned __int32 ERuint;
typedef float ERfloat;
typedef unsigned __int32 ERflags;
typedef unsigned __int32 ERbool;

#define ER_FALSE (0u)
#define ER_TRUE  (!ER_FALSE)

typedef void *ERContext;

typedef ERuint ERHandle;
typedef ERHandle ERTexture2D;

#define ER_INVALID_HANDLE ((ERHandle)0)

#define ER_MAX_ENUM (0x7FFFFFFF)

typedef enum
{
	ER_RESULT_OK = 0,
	ER_RESULT_INVALID_ARGUMENT,
	ER_RESULT_OUT_OF_MEMORY,
	
	ER_FORCE_RESULT_SIZE = ER_MAX_ENUM,
} ERResult;

typedef enum
{
	ER_PRIMITIVE_TRIANGLES,
	ER_PRIMITIVE_TRIANGLE_STRIP,
	ER_PRIMITIVE_QUADS,

	ER_FORCE_PRIMITIVE_SIZE = ER_MAX_ENUM,
} ERPrimitive;

typedef enum
{
	ER_MATRIX_MODELVIEW,
	ER_MATRIX_PROJECTION,
	ER_MATRIX_TEXTURE_0,
	ER_MATRIX_TEXTURE_1,

	ER_FORCE_MATRIX_SIZE = ER_MAX_ENUM,
} ERMatrix;

typedef enum
{
	ER_MATRIX_STORAGE_ROW_MAJOR,
	ER_MATRIX_STORAGE_COLUMN_MAJOR,
	
	ER_FORCE_MATRIX_STORAGE_SIZE = ER_MAX_ENUM,
} ERMatrixStorage;

typedef enum
{
	ER_FRONT_FACE_CCW,
	ER_FRONT_FACE_CW,
	
	ER_FORCE_FRONT_FACE_SIZE = ER_MAX_ENUM,
} ERFrontFace;

typedef enum
{
	ER_CULL_FACE_NONE,
	ER_CULL_FACE_BACK,
	ER_CULL_FACE_FRONT,
	ER_CULL_FACE_BOTH,

	ER_FORCE_CULL_FACE_SIZE = ER_MAX_ENUM,
} ERCullFace;

typedef enum
{
	ER_FORMAT_RGB8,
	ER_FORMAT_RGBA8,
} ERFormat;

#define ER_API __stdcall

ERResult ER_API erInitialize();
ERResult ER_API erShutdown();

ERResult ER_API erCreateContext(ERuint width, ERuint height, void *display, ERContext *context);
ERResult ER_API erDestroyContext(ERContext context);
ERResult ER_API erCreateTexture2D(ERContext context, ERuint width, ERuint height,
	ERFormat format, ERTexture2D *texture);
ERResult ER_API erDestroyTexture2D(ERContext context, ERTexture2D texture);

ERResult ER_API erUploadTexture2D(ERContext context, ERTexture2D texture, ERuint mip,
	const void *texels);

ERResult ER_API erClearColorBuffer(ERContext context, ERfloat red, ERfloat green, ERfloat blue, ERfloat alpha);
ERResult ER_API erClearDepthBuffer(ERContext context, ERfloat depth);

ERResult ER_API erSetViewport(ERContext context, ERint x, ERint y, ERuint width, ERuint height);
ERResult ER_API erSetMatrix(ERContext context, ERMatrix id, ERMatrixStorage storage, const ERfloat *matrix);
ERResult ER_API erSetDepthTest(ERContext context, ERbool enable);
ERResult ER_API erSetDepthMask(ERContext context, ERbool enable);
ERResult ER_API erSetFrontFace(ERContext context, ERFrontFace frontFace);
ERResult ER_API erSetCullFace(ERContext context, ERCullFace cullFace);
ERResult ER_API erSetTexture(ERContext context, ERuint slot, ERTexture2D texture);

ERResult ER_API erSetPositionArray(ERContext context, ERuint stride, const ERfloat *positions);
ERResult ER_API erSetTexCoordArray(ERContext context, ERuint stride, const ERfloat *texCoords, ERuint slot);
ERResult ER_API erSetColorArray(ERContext context, ERuint stride, const ERuint *colors);
ERResult ER_API erDraw(ERContext context, ERPrimitive primitive, ERuint vertexCount, ERuint offset);
ERResult ER_API erDrawIndexed(ERContext context, ERPrimitive primitive, ERushort indexCount, ERuint offset, const ERushort *indices);

ERResult ER_API erSwapBuffers(ERContext context);

#ifdef __cplusplus
}
#endif

#endif /* EDURENDER_ER_H */
